package com.wpml.ware.controller;

import com.wpml.common.util.PageUtils;
import com.wpml.common.util.ResultJson;
import com.wpml.ware.entity.PurchaseModel;
import com.wpml.ware.service.PurchaseService;
import com.wpml.ware.vo.MergeVo;
import com.wpml.ware.vo.PurchaseDoneVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 采购信息
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2019-11-17 13:50:10
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Resource
    private PurchaseService purchaseService;

    ///ware/purchase/done
    @PostMapping("/done")
    public ResultJson finish(@RequestBody PurchaseDoneVo doneVo){

        purchaseService.done(doneVo);

        return ResultJson.ok();
    }

    /**
     * 领取采购单
     * @return
     */
    @PostMapping("/received")
    public ResultJson received(@RequestBody List<Long> ids){

        purchaseService.received(ids);

        return ResultJson.ok();
    }

    ///ware/purchase/unreceive/list
    ///ware/purchase/merge
    @PostMapping("/merge")
    public ResultJson merge(@RequestBody MergeVo mergeVo){

        purchaseService.mergePurchase(mergeVo);
        return ResultJson.ok();
    }

    @GetMapping("/unreceive/list")
    //@RequiresPermissions("ware:purchase:list")
    public ResultJson unreceivelist(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPageUnreceivePurchase(params);

        return ResultJson.ok(page);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("ware:purchase:list")
    public ResultJson list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return ResultJson.ok(page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("ware:purchase:info")
    public ResultJson info(@PathVariable("id") Long id){
		PurchaseModel purchase = purchaseService.getById(id);

        return ResultJson.ok(purchase);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("ware:purchase:save")
    public ResultJson save(@RequestBody PurchaseModel purchase){
        purchase.setUpdateTime(LocalDateTime.now());
        purchase.setCreateTime(LocalDateTime.now());
		purchaseService.save(purchase);

        return ResultJson.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("ware:purchase:update")
    public ResultJson update(@RequestBody PurchaseModel purchase){
		purchaseService.updateById(purchase);

        return ResultJson.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("ware:purchase:delete")
    public ResultJson delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return ResultJson.ok();
    }

}
